package com.xindaoapp.happypet.activity;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.bean.GoodsInfo;

/**
 * 评价列表
 * 
 * @author Administrator
 * 
 */
public class EvaluateActivity extends BaseActivity {
    private Context mContext;
    private List<GoodsInfo> info;

    private ImageButton back;
    private LinearLayout ll;
    private String time;

    @SuppressWarnings("unchecked")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_evaluate);
        info = (List<GoodsInfo>) getIntent().getSerializableExtra("list");
        time = getIntent().getStringExtra("time");
        mContext = EvaluateActivity.this;

        initView();
        initData();
    }

    private void initView() {

        back = (ImageButton) findViewById(R.id.cancel);
        ll = (LinearLayout) findViewById(R.id.evaluate_content);

    }

    private void initData() {
        back.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                finish();
            }
        });

        ll.removeAllViews();
        for (int i = 0; i < info.size(); i++) {
            View view = View.inflate(mContext, R.layout.common_order_product_infor, null);
            ImageView icon = (ImageView) view.findViewById(R.id.order_icon);
            TextView title = (TextView) view.findViewById(R.id.order_title);
            TextView category = (TextView) view.findViewById(R.id.order_category);
            TextView num = (TextView) view.findViewById(R.id.order_num);
            TextView price = (TextView) view.findViewById(R.id.order_price);
            View line = view.findViewById(R.id.line);

            num.setText("×" + info.get(i).goods_number);
            num.setText(changeTextColor(num, 1, 1, "#D95234"));
            ImageLoader.getInstance().displayImage(info.get(i).goods_thumb, icon);
            title.setText(info.get(i).goods_name);
            category.setText("分类:" + info.get(i).goods_weight);
            price.setText("￥" + String.valueOf(info.get(i).goods_price));

            if (i == info.size() - 1) {
                line.setVisibility(View.GONE);
            }
            ll.addView(view);
            final int j = i;

            view.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (info.get(j).is_commented == 0) {
                        Intent intent =
                                new Intent(EvaluateActivity.this, OrderEvaluateActivity.class);
                        intent.putExtra("object", info.get(j));
                        intent.putExtra("time", time);
                        startActivity(intent);
                    } else {
                        Toast.makeText(mContext, "商品已经评价过！", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    }

    private SpannableStringBuilder changeTextColor(TextView tv, int start, int end, String color) {
        SpannableStringBuilder builder = new SpannableStringBuilder(tv.getText().toString());
        ForegroundColorSpan redSpan = new ForegroundColorSpan(Color.parseColor(color));
        builder.setSpan(redSpan, start, start + end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return builder;
    }

}
